﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using NHibernate;
using FoundationLib.Data.NHibernate;

namespace FoundationLib.Web.NHibernate
{
	/// <summary>
	/// An NHibernate session module.
	/// </summary>
	public class NHibernateSessionModule : IHttpModule
	{
		/// <see cref="IDisposable.Dispose()"/>
		public void Dispose()
		{
		}

		/// <see cref="IHttpModule.Init(HttpApplication)"/>
		public void Init(HttpApplication context)
		{
			context.BeginRequest += new EventHandler(BeginRequestHandler);
			context.EndRequest += new EventHandler(EndRequestHandler);
		}

		/// <see cref="IHttpModule.EndRequestHandler(object, EventArgs)"/>
		private void EndRequestHandler(object sender, EventArgs e)
		{
			NHibernateManager.SessionManager.Unbind();
		}

		/// <see cref="IHttpModule.BeginRequestHandler(object, EventArgs)"/>
		private void BeginRequestHandler(object sender, EventArgs e)
		{
			NHibernateManager.SessionManager.Bind();
		}
	}
}
